// dao/userDao.js
// 实现与MySQL交互
var mysql = require('mysql');
var Promise = require('promise');
var $conf = require('./db');
var $sql = require('./userSqlMapping');


console.log($conf.mysql);

// 使用连接池，提升性能
var pool  = mysql.createPool($conf.mysql);

pool.getConnection(function(err, connection) {
  // connected! (unless `err` is set) 
  if(err){
  	throw err
  }else{
  	console.log("connect mysql success");
  }
});

// 向前台返回JSON方法的简单封装
var jsonWrite = function (res, ret) {
	if(typeof ret === 'undefined') {
		res.json({
			succ: false,
			code:'1',
			msg: '操作失败'
		});
	} else {
		res.json(ret);
	}
};
Date.prototype.format = function(date){
	return this.getFullYear()+'-'+(this.getMonth()+1)+'-'+this.getDate()+' '+this.getHours()+':'+this.getMinutes()+':'+this.getSeconds();
}

var userDao = {
	pool:pool,
	callback:function(res,content,success){
		if(!success) {
			res.json({
				success: false,
				message: content,
				value:[]
			});
		} else {
			res.json({
				success: true,
				value: content
			});
		}
	},
	open:function(){
		var $this = this;
		var q = new Promise(function(resolve,reject){
			$this.pool.getConnection(function(err, connection) {
			  // connected! (unless `err` is set) 
			  if(err){
			  	reject(err);
			  	throw err		  	
			  }else{
			  	// console.log("connect mysql open success");
			  	resolve(connection);
			  }
			});
		});
		return q;
	},
	colse:function(connection){
		connection.release();
	},
	insert:function(req,res,next){
		
		var time =new Date().format();
		var $this = this;
		this.open().then(function(con){
			var param = req.body;
			con.query($sql.insert,[param.name,param.password,param.email,time,time],function(err,result){
				if(err){
					$this.callback(res, '数据库连接异常', false);
					throw err	
				}
				if(result){
					console.log(result);
					
					$this.callback(res,result,true);			
					
				}else{
					$this.callback(res,'注册失败',false);
				}
				$this.colse(con);
			});
			
		}).catch(function(err){
			$this.callback(res, '数据库连接异常', false);
		});
	},
	delete:function(){},
	update:function(){},
	login:function(req,res,next){
		var $this = this;
		this.open().then(function(con){
			var param = req.body;
			con.query($sql.login,[param.name,param.password],function(err,result){
				if(err){
					$this.callback(res, '数据库连接异常', false);
					throw err	
				}
				if(result){
					if(result.length == 0){
						$this.callback(res,'密码错误',false);
					}else{
						console.log(result);
						$this.callback(res,result,true);
						req.session.loged=true;
						req.session.user = result;			
					}
					
				}else{
					$this.callback(res,'密码错误',false);
				}
				$this.colse(con);
			});
			
		}).catch(function(err){
			$this.callback(res, '数据库连接异常', false);
		});
	},
	queryById:function(req,res,next){
		var $this = this;
		this.open().then(function(con){
			var param = req.body;
			con.query($sql.queryById,[param.id],function(err,result){
				console.log('this------------------------------');
				console.log($this);
				if(err){
					$this.callback(res, '数据库连接异常', false);
					throw err	
				}
				if(result){
					$this.callback(res,result,true);
				}else{
					$this.callback(res,'不存在该账号',false);
				}
				$this.colse(con);
			});
			
		}).catch(function(err){
			$this.callback(res, '数据库连接异常', false);
		});
	},
	queryByName:function(req,res,next){
		var $this = this;
		this.open().then(function(con){
			var param = req.body;
			con.query($sql.queryByName,[param.name],function(err,result){
				console.log('this------------------------------');
				console.log($this);
				if(err){
					$this.callback(res, '数据库连接异常', false);
					throw err	
				}
				if(result){
					$this.callback(res,result,true);
				}else{
					$this.callback(res,'不存在该账号',false);
				}
				$this.colse(con);
			});
			
		}).catch(function(err){
			$this.callback(res, '数据库连接异常', false);
		});
		
	},
	queryAll:function(req,res,next){
		var $this = this;
		this.open().then(function(con){
			
			con.query($sql.queryAll,function(err,result){
				console.log('this------------------------------');
				console.log($this);
				if(err){
					$this.callback(res, '数据库连接异常', false);
					throw err	
				}
				if(result){					
					$this.callback(res,result,true);
				}else{
					$this.callback(res,'不存在该账号',false);
				}
				$this.colse(con);
			});
			
		}).catch(function(err){
			$this.callback(res, '数据库连接异常', false);
		});
	}
}

module.exports = userDao;
